package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingProjectServiceImpl implements NursingProjectService {

    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    //查询 所有护理项目
    @Override
    public List seleteAll() {
        List<NursingProject> list =nursingProjectMapper.selectAll();
        List dtoList =  new ArrayList();
        list.forEach(s->{
            NursingProjectDto temp = BeanUtil.toBean(s, NursingProjectDto.class);
            dtoList.add(temp);
        });
        return dtoList;
    }

    //分页查询护理项目
    @Override
    public PageResponse<NursingProjectVo> selectByPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingProjectVo> page = nursingProjectMapper.selectByPage(name, status);
        PageResponse<NursingProjectVo> of = PageResponse.of(page, NursingProjectVo.class);
        of.getRecords().forEach(vo -> {
            long count = nursingProjectMapper.getProjectRefCount(vo.getId());
            vo.setCount(count);
        });
        System.err.println(page);
        return of;
    }

    //根据id查询护理项目
    @Override
    public NursingProjectVo selectById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.selectById(id);
        NursingProjectVo nursingProjectVo = BeanUtil.toBean(nursingProject, NursingProjectVo.class);
        return nursingProjectVo;
    }

    //新增护理项目
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        try {
            nursingProjectMapper.insertNursingProject(nursingProject);
        } catch (Exception e) {
            throw new BaseException(BasicEnum.NURSING_PROJECT_INSERT_FAIL);
        }
    }

    //    需删除护理项目id
    @Override
    public void deleteNursingProject(Long id) {
        long count = nursingProjectMapper.getProjectRefCount(id);
        if (count == 0) {
            nursingProjectMapper.deleteNursingProject(id);
        }
    }

    //修改护理项目状态接口
    @Override
    public void updateStatus(Long id, Integer status) {
        nursingProjectMapper.updateStatus(id,status);
    }

    //修改护理项目
    @Override
    public void update(NursingProjectDto dto) {
        NursingProject nursingProject = BeanUtil.toBean(dto, NursingProject.class);
        nursingProjectMapper.update(nursingProject);
    }
}
